
use anyhow::Result;
use hyper::{Client, client::HttpConnector};
use std::collections::HashMap;

use serde::{Deserialize, Serialize};

use crate::rpc::{JsonRpcRequest, jsonrpc_request};
use crate::miner::types::TaskType;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct Sector {
    pub number: u64,
    pub miner: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct CallId {
    #[serde(rename(deserialize = "ID"))]
    pub id: String,
    pub sector: Sector,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct SealWork {
    #[serde(rename(deserialize = "ID"))]
    pub id: CallId,
    pub sector: Sector,
    pub task: TaskType,
    pub run_wait: i32,
    pub start: String,
}

pub async fn query_jobs_from_miner(client: &Client<HttpConnector>, url: &str, token: &str) -> Result<HashMap<String, Vec<SealWork>>>{
    let req = JsonRpcRequest::with_params("Filecoin.WorkerJobs", vec![]);
    let r = jsonrpc_request::<HashMap<String, Vec<SealWork>>>(client, url, token, req).await?;
    match r {
        None => Err(anyhow::anyhow!("worker jobs not found.")),
        Some(v) => Ok(v)
    }
}